package cn.yeziji.forum.config.auth;

import cn.yeziji.forum.converter.UserConverter;
import cn.yeziji.forum.dao.intf.ForumUserDao;
import cn.yeziji.forum.entity.ForumUserEntity;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 自定义用户信息
 *
 * @author gzkemays
 * @since 2021/12/7 9:37
 */
@Component
public class MemberUserDetailServiceImpl implements UserDetailsService {
  @Resource ForumUserDao userDao;

  @Override
  public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
    ForumUserEntity userEntity = userDao.selectUserByUserName(username);
    if (userEntity == null) {
      return null;
    }
    /*    if (Objects.isNull(userEntity)) {
      Object ifPresent = filterLoginCache.getIfPresent(username);
      if (ifPresent instanceof UserRegisterBO) {
        UserRegisterBO bo = (UserRegisterBO) ifPresent;
        bo.setUsername(username);
        bo.setPassword(PasswordSecurity.generatedPassword(bo.getPassword()));
        // 登录与携同时，返回直接注册的对象。
        return UserConverter.INSTANCE.entityConvertToAuth(userDao.registerUser(bo));
      }
    }*/
    return UserConverter.INSTANCE.entityConvertToAuth(
        userDao.login(username, userEntity.getPassword()));
  }
}
